package com.cino.org.controller;


import com.cino.basic.util.AjaxResult;
import com.cino.basic.util.PageList;
import com.cino.org.domain.Department;
import com.cino.org.domain.Employee;
import com.cino.org.query.EmployeeQuery;
import com.cino.org.service.DepartmentService;
import com.cino.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "员工的API",description="员工相关的CRUD功能")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Employee loadById(@PathVariable("id") Long id){
        return employeeService.loadById(id);
    }

    @ApiOperation(value = "查询所有" )
    @GetMapping
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    @ApiOperation(value = "通过ID删除" )
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @ApiOperation(value = "添加或修改" )
    @PutMapping
    public AjaxResult addorUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.save(employee);
            }else{
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @ApiOperation(value = "分页查询" )//高级查询和分页查询都用的post
    @PostMapping
    public PageList<Employee> pageQuery(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.pageQuery(employeeQuery);
    }

    @ApiOperation(value ="接口：批量删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            //1.调用service的方法
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }

    }

    @ApiOperation(value = "接口：部门无限极树")
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

    /**
     *指定店铺的所有管理人员
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    public List<Employee> queryByShopId(@PathVariable("shopId") Long shopId){
        return employeeService.queryByShopId(shopId);
    }
}
